package com.yanqu.road.server.manger.cricket.skillhandler.attround.handler;

import com.yanqu.road.entity.cricket.helper.AddHpResult;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketGame;
import com.yanqu.road.server.gameplayer.module.cricket.battle.eCricketBattleRoundStep;
import com.yanqu.road.server.gameplayer.module.cricket.model.BattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.SkillData;
import com.yanqu.road.server.manger.cricket.skillhandler.attround.CricketSkillAttRoundStrategy;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CricketSkill149Strategy implements CricketSkillAttRoundStrategy {

    /**
     * 技能处理-攻击{0}次后，投掷尚方宝剑造成{1}攻击力的伤害。
     * @param cricketGame 本局对局对象
     * @param skillData 触发的技能
     * @param baseAttSkill 基础攻击技能
     * @param baseAttReHpResult 基础攻击结果对象
     * @param action 我方
     * @param target 对方
     * @param attackRoundBuilder 返回PB结构体
     * @return true：死亡，退出技能处理，false：继续技能处理
     */
    @Override
    public boolean skillHandle(CricketGame cricketGame, SkillData skillData, SkillData baseAttSkill, AddHpResult baseAttReHpResult, BattleData action, BattleData target, CricketProto.CricketAttackRound.Builder attackRoundBuilder, long battleFrameId) {
        int attackTimes = baseAttSkill.getSkillEffect().getP2();
        long baseAttack = action.getAttack(target);
        //基础攻击非miss，增加次数
        if (baseAttReHpResult != null) {
            skillData.setOverlay(skillData.getOverlay() + attackTimes);
        }
        if (skillData.getOverlay() >= skillData.getSkillEffect().getP2()) {
            skillData.effectAmountInc();
            skillData.setOverlay(skillData.getOverlay() - skillData.getSkillEffect().getP2());
            CricketProto.CricketBattleFrame.Builder battleFrameBuilder = cricketGame.newBattleFrameBuilder();
            battleFrameBuilder.setActionUserId(action.getUserId());
            battleFrameBuilder.setTargetUserId(target.getUserId());
            battleFrameBuilder.setSkillId(skillData.getSkillId());
            long attack = BigDecimal.valueOf(baseAttack).multiply(BigDecimal.valueOf(skillData.getSkillEffect().getP1())).divide(BigDecimal.valueOf(1000), 8, RoundingMode.DOWN).longValue();
            AddHpResult addHpResult1 = target.addHp(-attack);
            battleFrameBuilder.setAttack(addHpResult1.getValue());
            battleFrameBuilder.setIsKill(addHpResult1.isKill());
            battleFrameBuilder.setHarmEffectSkillId(addHpResult1.getReduceHpEffectSkillId());
            battleFrameBuilder.setIsMiss(addHpResult1.isMissFlag());
            cricketGame.fillRemainHp(battleFrameBuilder);
            attackRoundBuilder.addAttack(battleFrameBuilder);
            if (cricketGame.isOver(attackRoundBuilder, eCricketBattleRoundStep.AfterAttack.getValue(), battleFrameBuilder.getBattleFrameId())) {
                return true;
            }
        }
        return false;
    }
}
